/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import DTO.*;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author QuangHau
 */
public class XpathDAO {

    public static ArrayList<XpathDTO> selectXpathAll() {
        Connection con = DataProvider.Connection();
        if (con != null) {
            try {
                CallableStatement cs = con.prepareCall("{call usp_SelectXpath}");
                ResultSet rs = cs.executeQuery();
                ArrayList<XpathDTO> arr = new ArrayList<XpathDTO>();
                while (rs.next()) {
                    XpathDTO item = new XpathDTO();
                    item.setMaXpath(rs.getInt("maxpath"));
                    item.setTenXpath(rs.getString("tenxpath"));
                    item.setLinkTieuDe(rs.getString("linktieude"));
                    item.setTinTuc(rs.getString("tintuc"));
                    item.setTieuDe(rs.getString("tieude"));
                    item.setHinhAnh(rs.getString("hinhanh"));
                    item.setNgayDang(rs.getString("ngaydang"));
                    item.setMoTa(rs.getString("mota"));
                    item.setTrang(rs.getString("trang"));
                    item.setMaChuDe(rs.getInt("machude"));
                    item.setTenChuDe(rs.getString("tenchude"));
                    item.setNgayBatDau(rs.getTimestamp("ngaybatdau"));
                    item.setLinkChuDe(rs.getString("linkchude"));
                    item.setMaWebsite(rs.getInt("mawebsite"));
                    item.setTenWebsite(rs.getString("tenwebsite"));
                    arr.add(item);
                }
                con.close();
                return arr;
            } catch (SQLException ex) {
                Logger.getLogger(ChuDeDAO.class.getName()).log(Level.SEVERE, null, ex);
                return null;
            }
        }
        return null;
    }

    public static ArrayList<XpathDTO> selectByChuDe(int maChuDe) {
        Connection con = DataProvider.Connection();
        if (con != null) {
            try {
                CallableStatement cs = con.prepareCall("{call usp_SelectXpath(?)}");
                cs.setInt("machude", maChuDe);
                ResultSet rs = cs.executeQuery();
                ArrayList<XpathDTO> arr = new ArrayList<XpathDTO>();
                while (rs.next()) {
                    XpathDTO item = new XpathDTO();
                  item.setMaXpath(rs.getInt("maxpath"));
                    item.setTenXpath(rs.getString("tenxpath"));
                    item.setLinkTieuDe(rs.getString("linktieude"));
                    item.setTinTuc(rs.getString("tintuc"));
                    item.setTieuDe(rs.getString("tieude"));
                    item.setHinhAnh(rs.getString("hinhanh"));
                    item.setNgayDang(rs.getString("ngaydang"));
                    item.setMoTa(rs.getString("mota"));
                    item.setTrang(rs.getString("trang"));
                    item.setMaChuDe(rs.getInt("machude"));
                    item.setTenChuDe(rs.getString("tenchude"));
                    item.setNgayBatDau(rs.getTimestamp("ngaybatdau"));
                    item.setLinkChuDe(rs.getString("linkchude"));
                    item.setMaWebsite(rs.getInt("mawebsite"));
                    item.setTenWebsite(rs.getString("tenwebsite"));
                    arr.add(item);
                }
                con.close();
                return arr;
            } catch (SQLException ex) {
                Logger.getLogger(ChuDeDAO.class.getName()).log(Level.SEVERE, null, ex);
                return null;
            }
        }
        return null;
    }

    public static XpathDTO ThongTinXpath(int maXpath) {
        Connection con = DataProvider.Connection();
        if (con != null) {
            try {
                CallableStatement cs = con.prepareCall("{call usp_SelectXpath(?,?)}");
                cs.setObject("machude", null);
                cs.setInt("maxpath", maXpath);
                ResultSet rs = cs.executeQuery();
                XpathDTO xpathDTO = new XpathDTO();
                if (rs.next()) {
                    xpathDTO.setMaXpath(rs.getInt("maxpath"));
                    xpathDTO.setTenXpath(rs.getString("tenxpath"));
                    xpathDTO.setLinkTieuDe(rs.getString("linktieude"));
                    xpathDTO.setTinTuc(rs.getString("tintuc"));
                    xpathDTO.setTieuDe(rs.getString("tieude"));
                    xpathDTO.setHinhAnh(rs.getString("hinhanh"));
                    xpathDTO.setNgayDang(rs.getString("ngaydang"));
                    xpathDTO.setMoTa(rs.getString("mota"));
                    xpathDTO.setTrang(rs.getString("trang"));
                    xpathDTO.setMaChuDe(rs.getInt("machude"));
                    xpathDTO.setTenChuDe(rs.getString("tenchude"));
                    xpathDTO.setNgayBatDau(rs.getTimestamp("ngaybatdau"));
                    xpathDTO.setLinkChuDe(rs.getString("linkchude"));
                    xpathDTO.setMaWebsite(rs.getInt("mawebsite"));
                    xpathDTO.setTenWebsite(rs.getString("tenwebsite"));

                }
                con.close();
                return xpathDTO;
            } catch (SQLException ex) {
                Logger.getLogger(ChuDeDAO.class.getName()).log(Level.SEVERE, null, ex);
                return null;
            }
        }
        return null;
    }

    public static boolean insertXpath(XpathDTO xpathDTO) {
        Connection con = DataProvider.Connection();
        if (con != null) {
            try {
                CallableStatement cs = con.prepareCall("{call usp_InsertXpath(?,?,?,?,?,?,?,?,?)}");
                cs.setString("tenxpath", xpathDTO.getTenXpath());
                cs.setString("linktieude", xpathDTO.getLinkTieuDe());
                cs.setString("tintuc", xpathDTO.getTinTuc());
                cs.setString("tieude", xpathDTO.getTieuDe());
                cs.setString("hinhanh", xpathDTO.getHinhAnh());
                cs.setString("ngaydang", xpathDTO.getNgayDang());
                cs.setString("mota", xpathDTO.getMoTa());
                cs.setString("trang", xpathDTO.getTrang());
                cs.setInt("machude", xpathDTO.getMaChuDe());
                int rs = cs.executeUpdate();
                if (rs > 0) {
                    return true;
                }
                return false;
            } catch (SQLException ex) {
                Logger.getLogger(ChuDeDAO.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }
        }
        return false;
    }

    public static boolean updateXpath(XpathDTO xpathDTO) {
        Connection con = DataProvider.Connection();
        if (con != null) {
            try {
                CallableStatement cs = con.prepareCall("{call usp_UpdateXpath(?,?,?,?,?,?,?,?,?,?)}");
                cs.setInt("maxpath", xpathDTO.getMaXpath());
                cs.setString("tenxpath", xpathDTO.getTenXpath());
                cs.setString("linktieude", xpathDTO.getLinkTieuDe());
                cs.setString("tintuc", xpathDTO.getTinTuc());
                cs.setString("tieude", xpathDTO.getTieuDe());
                cs.setString("hinhanh", xpathDTO.getHinhAnh());
                cs.setString("ngaydang", xpathDTO.getNgayDang());
                cs.setString("mota", xpathDTO.getMoTa());
                cs.setString("trang", xpathDTO.getTrang());
                cs.setInt("machude", xpathDTO.getMaChuDe());
                int rs = cs.executeUpdate();
                if (rs > 0) {
                    return true;
                }
                return false;
            } catch (SQLException ex) {
                Logger.getLogger(ChuDeDAO.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }
        }
        return false;
    }

    public static boolean deleteXpath(int maXpath) {
        Connection con = DataProvider.Connection();
        if (con != null) {
            try {
                CallableStatement cs = con.prepareCall("{call usp_DeleteXpath(?)}");
                cs.setInt("maxpath", maXpath);
                int rs = cs.executeUpdate();
                if (rs > 0) {
                    return true;
                }
                return false;
            } catch (SQLException ex) {
                Logger.getLogger(ChuDeDAO.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }
        }
        return false;
    }
//    public static void main (String[] arg)
//    {
//        XpathDTO t = new XpathDTO();
//        t.setMaXpath(2);
//        t.setTenXpath("tên xpath");
//        t.setGhiChu("ghi chú");
//        t.setTinTuc("tin tức");
//        t.setTieuDe("tieude");
//        t.setHinhAnh("hình ảnh");
//        t.setNgayDang("ngày đăng");
//        t.setMoTa("mota");
//        t.setTrang("trang");
//        t.setMaChuDe(1);
//        if(deleteXpath(1))
//            System.out.println(selectAll().size());
//    }
}
